package com.ymatou.iapi.trading.operate.parameter;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.ymatou.iapi.trading.operate.parameter.enums.BizTypeEnum;
import com.ymatou.iapi.trading.operate.parameter.enums.LogisticsTypeEnum;
import com.ymatou.iapi.trading.operate.parameter.enums.OrderSourceEnum;
import com.ymatou.iapi.trading.operate.parameter.req.BizVo;
import com.ymatou.iapi.trading.operate.parameter.req.ChangeOrderBizStatusBean;
import com.ymatou.iapi.trading.operate.parameter.req.PlaceOrderReq;
import com.ymatou.iapi.trading.operate.service.ChangeOrderBizStatusCall;
import com.ymt.core.tool.Logger;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.utils.TestDataManager;

/**
 * 
 * 砍价团服务
 *
 */
public class BargainGroupService {

	/**
	 * 开团
	 * 
	 * @param bizId
	 *            =null,随机生成一个砍价团id
	 * @return 团id
	 * */
	public static PlaceOrderResp openGroup(String bizId) {

		// 砍价团下单
		PlaceOrderResp placeOrder = PlaceOrderService.placeOrder1(bizId);

		int tradingId = placeOrder.getTradingId();
		Integer orderId = placeOrder.getOrderList().get(0).getOrderId();
		BigDecimal payAmount = placeOrder.getPayAmount();
		int userId = placeOrder.getUserId();

		// 支付订单
		new OrderCallService().orderPayRequest(userId, payAmount, tradingId);

		return placeOrder;

	}
	/**
	 * 参团
	 * 
	 * */
	public static PlaceOrderResp joinGroup(String bizId) {

		// 砍价团参团订单
		PlaceOrderResp placeOrder = PlaceOrderService.placeOrder2(bizId);

		int tradingId = placeOrder.getTradingId();
		Integer orderId = placeOrder.getOrderList().get(0).getOrderId();
		int userId = placeOrder.getUserId();
		BigDecimal payAmount = placeOrder.getPayAmount();

		// 参团支付
		new OrderCallService().orderPayRequest(userId, payAmount, tradingId);

		return placeOrder;

	}
	/**
	 * 创建一个虚拟商品订单
	 * 
	 * */
	public static PlaceOrderResp placeVirtualProduct() {

		PlaceOrderReq placeorderBean = PlaceOrderService.getDefaultOrderReq();

		placeorderBean.setYmatouCoupon(null);

		BizVo bizVo = new BizVo();

		// 虚拟商品
		int bizType = BizTypeEnum.VIRTUAL_PRODUCT.getCode();

		String bizId = TestDataManager.getRandomNum(5);

		bizVo.setBizId(bizId);
		bizVo.setBizType(bizType);

		placeorderBean.setBizSource(bizVo);
		//物流方式

		OrderVo orderVo = placeorderBean.getOrderGroups().get(0).getOrders()
				.get(0);
		orderVo.getOrderItems().get(0)
				.setLogisticsType(LogisticsTypeEnum.AutoDelivery);

		placeorderBean.getOrderGroups().get(0).setSellerCoupon(null);

		return PlaceOrderService.callPlaceOrderService(placeorderBean);
	}

	/**
	 * 创建一个微店商品订单
	 * 
	 * */
	public static PlaceOrderResp placeLiveWechar(LogisticsTypeEnum type) {

		PlaceOrderReq placeorderBean = PlaceOrderService.getDefaultOrderReq();

		placeorderBean.setYmatouCoupon(null);

		placeorderBean.setOrderSource(OrderSourceEnum.WechatMP);
		BizVo bizVo = new BizVo();

		// 虚拟商品
		int bizType = BizTypeEnum.LIVE_WECHAT.getCode();

		String bizId = TestDataManager.getRandomNum(5);

		bizVo.setBizId(bizId);
		bizVo.setBizType(bizType);

		placeorderBean.setBizSource(bizVo);
		//物流方式

		OrderVo orderVo = placeorderBean.getOrderGroups().get(0).getOrders()
				.get(0);
		orderVo.getOrderItems().get(0).setLogisticsType(type);

		placeorderBean.getOrderGroups().get(0).setSellerCoupon(null);

		return PlaceOrderService.callPlaceOrderService(placeorderBean);
	}
	/**
	 * 砍价团服务通知交易系统，团状态接口
	 * 
	 * @param bizId
	 *            团id
	 * @param orderId
	 *            订单号
	 * @param bizSubType
	 *            砍价团(1 开团, 2 参团)
	 * @param bizStatus
	 *            状态 砍价团(-3 组团失败, -2 参团失败, -1 开团失败, 0 默认砍价团, 1 开团成功, 2 参团成功, 3
	 *            组团成功)
	 * */
	public static void notifyOrderBizStatus(String bizId, Integer orderId,
			int bizSubType, int bizStatus) {
		ChangeOrderBizStatusBean changeorderbizstatusBean = new ChangeOrderBizStatusBean();
		ChangeOrderBizStatusCall changeorderbizstatusCall = new ChangeOrderBizStatusCall();
		try {
			BizVo bizVo = new BizVo();

			bizVo.setBizStatus(bizStatus);
			bizVo.setBizSubType(bizSubType);
			bizVo.setBizType(1);
			bizVo.setOrderId(orderId);
			bizVo.setBizId(bizId);
			List bizVos = new ArrayList<BizVo>();
			bizVos.add(bizVo);

			changeorderbizstatusBean.setBizVos(bizVos);
			changeorderbizstatusCall.setData(false, changeorderbizstatusBean);
			changeorderbizstatusCall.callService();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
